import PoolManager from "../pool/PoolManager";
import RandomHelper from "../tools/RandomHelper";

/**
 * cgw 20190505
 */
let offset:number[] = [-1,0,1]
export default class ActionHelper {
    private static offset: number[] = [-1, 1]
    /**
     * 发牌动画
     * @param node 扑克牌的节点
     * @param duration 延迟时间
     * @param dest 目标位置坐标
     * @param funcStart 动画开始播放时触发
     * @param funcEnd 动画结束时触发
     */
    static dealAction(node: cc.Node, duration: number, dest: cc.Vec2, funcStart?: Function, funcEnd?: Function) {
        let scale = node.scale;
        node.scale = 0.0;
        let dt = 0.5;
        let smr = cc.spawn([cc.scaleTo(dt, scale), cc.rotateBy(dt, 720), cc.moveTo(dt, dest)]);
        let list = [];
        if (duration > 0) {
            list.push(cc.delayTime(duration));
        }
        if (funcStart) {
            list.push(cc.callFunc(funcStart))
        }
        list.push(smr);
        if (funcEnd) {
            list.push(cc.callFunc(funcEnd))
        }
        node.runAction(cc.sequence(list));
        return dt;
    }

    static fanpaiAction(node: cc.Node, funcMid?: Function, funcEnd?: Function) {
        let oldScale = node.scaleX;
        let dt = 0.2;

        let list = [];
        list.push(cc.scaleTo(dt, 0, node.scaleY))
        if (funcMid) {
            list.push(cc.callFunc(funcMid))
        }
        list.push(cc.scaleTo(dt, oldScale, node.scaleY))

        if (funcEnd) {
            list.push(cc.callFunc(funcEnd))
        }

        node.runAction(cc.sequence(list));
        return dt * 2;
    }
    static fanpaiRepeatAction(node: cc.Node,dt:number = 0.5, funcMid?: Function, funcEnd?: Function) {
        let oldScale = node.scaleX;
        // let dt = 0.5;

        let list = [];
        list.push(cc.scaleTo(dt, 0, node.scaleY))
        if (funcMid) {
            list.push(cc.callFunc(funcMid))
        }
        list.push(cc.scaleTo(dt, oldScale, node.scaleY))
        list.push(cc.delayTime(3))

        if (funcEnd) {
            list.push(cc.callFunc(funcEnd))
        }

        node.runAction(cc.repeatForever(cc.sequence(list)));
        return dt * 2;
    }    

    static flyGold(node: cc.Node, duration: number, dest: cc.Vec2, funcStart?: Function, funcEnd?: Function): number {
        let dt = 0.5;
        let list = [];
        if (duration > 0) {
            list.push(cc.delayTime(duration));
        }
        if (funcStart) {
            list.push(cc.callFunc(funcStart, node))
        }
        list.push(cc.moveTo(dt, dest));
        if (funcEnd) {
            list.push(cc.callFunc(funcEnd, node))
        }
        node.runAction(cc.sequence(list));

        return dt;
    }


    static toastAction(node: cc.Node, func: Function, time: number = 1, dis: number = 200) {
        let delay = cc.delayTime(0.3);
        let moveBy = cc.moveBy(time, cc.v2(0, dis));
        let fadeOut = cc.fadeOut(time);
        let callback = cc.callFunc(func);
        node.runAction(cc.sequence(delay, cc.spawn(moveBy, fadeOut), callback));
    }

    static upAction(node: cc.Node, func: Function, time: number = 1, dis: number = 200) {
        let moveBy = cc.moveBy(time, cc.v2(0, dis));
        let callback = cc.callFunc(func);
        node.runAction(cc.sequence(moveBy, callback));
    }

    /**
     * 
     * @param node 
     * @param times 
     */
    static blinkAction(node: cc.Node, func?: Function, duration: number = 1, times: number = 3) {
        let blink = cc.blink(duration, times);
        if (func) {
            let list = [];
            list.push(blink);
            list.push(cc.callFunc(func));
            node.runAction(cc.sequence(list));
        } else {
            node.runAction(blink);
        }
    }

    static behit(node: cc.Node, func?: Function, duration: number = 0.1, times: number = 1) {
        if(!node){
            return;
        }
        node.stopAllActions();
        node.scale = 1;
        let scale1 = cc.scaleTo(duration, 1.3);
        let scale2 = cc.scaleTo(duration, 1)
        let list = [];
        list.push(scale1);
        list.push(scale2);
        if (func) {
            list.push(cc.callFunc(func));
        }
        node.runAction(cc.sequence(list));
    }

    static scaleAction(node: cc.Node, scale: number, func?: Function, duration: number = 1) {
        node.runAction(cc.sequence(cc.scaleTo(duration, scale), cc.callFunc(func)))
    }

    static divideAction(node: cc.Node, func: Function, dis: number = 30, duration: number = 0.05) {
        node.opacity = 255;
        let list = []
        let x = node.x;
        for (let index = 0; index < 6; index++) {
            const element = this.offset[index % 2];
            cc.log(" disdivideAction   ", element, dis, x + element * dis)
            list.push(cc.moveTo(duration, x + element * dis, node.y))
        }
        // list.push(cc.moveBy(0,0))
        list.push(cc.callFunc(func))
        node.runAction(cc.sequence(list))
    }

    static openView(node: cc.Node, delay: number = 0.2, func?: Function) {
        let list = []
        node.scale = 0;
        list.push(cc.scaleTo(delay, 1.2))
        list.push(cc.scaleTo(0.2, 1))
        if(func){
            list.push(cc.callFunc(func))
        }
 
        node.runAction(cc.sequence(list))
    }
    static closeView(node: cc.Node, delay: number = 0.2, func?: Function) {
        let list = []
        // node.scale = 0;
        list.push(cc.scaleTo(delay, 0))
        // list.push(cc.scaleTo(0.2,1))
        if(func){
            list.push(cc.callFunc(func))
        }
 
        node.runAction(cc.sequence(list))
    }

    static moveBy(node: cc.Node, x: number, y: number, func?: Function, duration: number = 0.5) {
        node.runAction(cc.sequence([cc.moveBy(duration, x, y), cc.callFunc(func)]))
    }

    static manyCall(node: cc.Node, func1: Function, func2: Function,func3:Function, delay: number = 0.1 , count: number = 3) {
        let list = []
        for (let index = 0; index < count; index++) {
            list.push(cc.callFunc(func1))
            list.push(cc.delayTime(delay))
            list.push(cc.callFunc(func2))
            list.push(cc.delayTime(delay))
        }
        list.push(cc.callFunc(func3))
        node.runAction(cc.sequence(list))
    }

    static fadeInView(node: cc.Node, duration: number = 1, func: Function) {
        node.opacity = 0;
        node.runAction(cc.sequence([cc.fadeIn(duration), cc.callFunc(func)]))
    }

    static fadeOutView(node: cc.Node, duration: number = 1, func: Function) {
        node.runAction(cc.sequence([cc.fadeOut(duration), cc.callFunc(func)]))
    }

    static shakeAction(node: cc.Node) {
        let rotataon1 = cc.rotateBy(0.1, 20)
        let rotation2 = cc.rotateBy(0.1, -20);
        let dur = cc.delayTime(3);
        let playCount = cc.repeat(cc.sequence(rotataon1, rotation2), 3)
        let sequnce = cc.sequence(playCount, dur)
        node.runAction(cc.repeatForever(sequnce))
    }

    static luckySpin(node: cc.Node, num: number, func: Function) {

        let r1 = cc.rotateBy(2, 1800)
        let stop = () => {
            node.rotation = 0;
        }
        let index = num - 1;
        let angle = 360 - index * 60;

        let r2 = cc.rotateBy(1, angle)
        node.runAction(cc.sequence(r1, cc.callFunc(stop), r2, cc.callFunc(func)))
    }


    static showGold(item: cc.Node, gold: cc.Node, func: Function) {
        let list = []
        for (let index = 0; index < 3; index++) {
            let scale = cc.scaleTo(0.1, 0, 1);
            let scale2 = cc.scaleTo(0.1, 1, 1);
            list.push(scale)
            list.push(scale2)
        }
        list.push(cc.moveTo(0.2, gold.getParent().convertToWorldSpaceAR(gold.position)))
        list.push(cc.callFunc(() => {
            this.behit(gold)
            func();
        }))
        item.runAction(cc.sequence(list))
    }


    static showGold2(item: cc.Node, gold: cc.Node, index,func: Function) {
        let list = []
        // for (let index = 0; index < 3; index++) {
        //     let scale = cc.scaleTo(0.1, 0, 1);
        //     let scale2 = cc.scaleTo(0.1, 1, 1);
        //     list.push(scale)
        //     list.push(scale2)
        // }
        item.scale = 1;
        let position = item.position;
        let x = position.x + offset[RandomHelper.random(0,offset.length)] * 50;
        let y = position.y + offset[RandomHelper.random(0,offset.length)] * 50;
        list.push(cc.moveTo(0.2,x,y))
        list.push(cc.delayTime(0.05*index))
        let worldPos = gold.getParent().convertToWorldSpaceAR(gold.position);
        // cc.log('gold.position ',gold.position,' worldPos ',worldPos)
        list.push(cc.spawn( cc.moveTo(0.5,worldPos),cc.scaleTo(0.5,0.5)))
        list.push(cc.callFunc(() => {
            this.behit(gold)
            func();
        }))
        item.runAction(cc.sequence(list))
    }    
}
